﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RFEngine.Storage
{
    public static class StorageContainer
    {
        #region Fields

#if WINDOWS
        private static string _CurrentDirectory = Environment.CurrentDirectory + "\\";

#elif XBOX
        private static string _CurrentDirectory = Microsoft.Xna.Framework.Storage.StorageContainer.TitleLocation + "\\";

#endif

        #endregion

        #region Properties

        /// <summary>
        /// Gets a string value of the Current working directory.
        /// </summary>
        public static string CurrentDirectory
        {
            get { return _CurrentDirectory; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Method for Creating a Directory.
        /// </summary>
        /// <param name="directoryPath">the final path of the directory to create.</param>
        public static void CreateDirectory(string directoryPath)
        {
            Directory.CreateDirectory(directoryPath);
        }

        /// <summary>
        /// Method for Generating a Directory structure.
        /// </summary>
        /// <param name="finalPath">The final path of the directory to create</param>
        public static void CreateDirectoryStructure(string finalPath)
        {
            string directoryPath = finalPath;
            directoryPath = directoryPath.Replace(_CurrentDirectory, string.Empty);
            string[] folders = directoryPath.Split('\\');

            string directoryToCheck = _CurrentDirectory;

            foreach (string item in folders)
            {
                directoryToCheck += @"\" + item;
                if (!DirectoryExists(directoryToCheck))
                    CreateDirectory(directoryToCheck);
            }
        }

        /// <summary>
        /// Method for Getting the Directory Path of where the file resides.
        /// </summary>
        /// <param name="fileLocation">a string value that represents the location and filename</param>
        /// <returns>Returns the Directory Path of the file</returns>
        public static string GetDirectoryPath(string fileLocation)
        {
            string location = fileLocation;
            location = location.Replace('/', '\\');

            int indexOf = location.LastIndexOf('\\') + 1;
            int length = location.Length;

            location = location.Remove(indexOf, length - indexOf);

            return location;
        }

        /// <summary>
        /// Method for Getting the Filename of the file in the filelocation.
        /// </summary>
        /// <param name="fileLocation">a string value that represents the location and filename</param>
        /// <returns>Returns a string value of the Filename</returns>
        public static string GetFileName(string fileLocation)
        {
            return GetFileName(fileLocation, false);
        }

        /// <summary>
        /// Method for Getting the Filename of the file in the filelocation.
        /// </summary>
        /// <param name="fileLocation">a string value that represents the location and filename</param>
        /// <param name="includeExtension">a bool value indicating whether to include the file extension with the file</param>
        /// <returns>Returns a string value of the Filename</returns>
        public static string GetFileName(string fileLocation, bool includeExtension)
        {
            string Path = GetDirectoryPath(fileLocation);
            string fileName = fileLocation.Remove(0, Path.Length);

            if (!includeExtension)
            {
                int lastIndex = fileName.LastIndexOf('.');
                int filenameLength = fileName.Length;

                string extension = fileName.Remove(0, lastIndex);
                int extensionLength = extension.Length;

                fileName = fileName.Remove(filenameLength - extensionLength, extensionLength);
            }

            return fileName;
        }

        /// <summary>
        /// Method for Getting the File Extension of a file.
        /// </summary>
        /// <param name="fileLocation">a string value that represents the location of the file</param>
        /// <returns>Returns a string vlaue of the the extension</returns>
        public static string GetFileExtension(string fileLocation)
        {
            string Path = GetDirectoryPath(fileLocation);
            string fileName = fileLocation.Remove(0, Path.Length);

            int lastIndex = fileName.LastIndexOf('.');

            return fileName.Remove(0, lastIndex + 1).ToLower();
        }

        /// <summary>
        /// Method for determining if a Directory Exists.
        /// </summary>
        /// <param name="directoryLocation">The path to the directory.</param>
        /// <returns>Returns true if directory exist otherwise false</returns>
        public static bool DirectoryExists(string directoryLocation)
        {
            return Directory.Exists(directoryLocation);
        }

        /// <summary>
        /// Method for determining if a file Exists.
        /// </summary>
        /// <param name="fileLocation">the file location</param>
        /// <returns>Returns true if the file exists otherwise false</returns>
        public static bool FileExists(string fileLocation)
        {
            return File.Exists(fileLocation);
        }

        #endregion
    }
}
